//
//  ViewController.m
//  JKNotification
//
//  Created by 王冲 on 2019/3/3.
//  Copyright © 2019年 JK科技有限公司. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, weak) id observe;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    // [self note1];
    // [self note2];
    // [self note3];
    [self note4];
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    /**
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        [[NSNotificationCenter defaultCenter]postNotificationName:@"note" object:nil];
        
    });
     */
    
     dispatch_sync(dispatch_get_global_queue(0, 0), ^{
     
        [[NSNotificationCenter defaultCenter]postNotificationName:@"note" object:nil];
     
     });
    
}

#pragma mark 3.2、异步通知的使用2
-(void)note4{

    self.observe = [[NSNotificationCenter defaultCenter]addObserverForName:@"note" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
            
            // 只要监听到通知就会被调用
            NSLog(@"当前的线程=%@",[NSThread currentThread]);
            
            NSLog(@"%@",self);
            
    }];
    
}

#pragma mark 3.1、异步通知的使用1
-(void)note3{
    
    // 监听通知：异步
    // 异步任务执行的顺序不确定
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(reciveNote:) name:@"note" object:nil];
    });
    
}

// 异步：监听通知，主线程：发出通知
// 总结：接收通知代码 由 发出通知线程决定
-(void)reciveNote:(NSNotification *)notify{
    
    NSLog(@"当前的线程=%@",[NSThread currentThread]);
    NSLog(@"通知");
    
    dispatch_sync(dispatch_get_main_queue(), ^{
        
        // 在此刷新UI
    });
    
}

#pragma mark 2、用block的通知
-(void)note2{
    
    /**
     name：通知名称
     object：谁发出的通知
     queue：决定block在哪个线程执行，nil：在发布通知的线程中执行
     usingBlock：只要监听到通知，就会执行该blocl
     - (id <NSObject>)addObserverForName:(nullable NSNotificationName)name object:(nullable id)obj queue:(nullable NSOperationQueue *)queue usingBlock:(void (^)(NSNotification *note))block API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
     */
    self.observe = [[NSNotificationCenter defaultCenter]addObserverForName:@"note" object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
        
        // 只要监听到通知就会被调用
        NSLog(@"当前的线程=%@",[NSThread currentThread]);
        
        NSLog(@"%@",self);
        
    }];
    
    /**
     发送通知
     aName:通知名称
     anObject:谁发出的通知
     - (void)postNotificationName:(NSNotificationName)aName object:(nullable id)anObject;
     */
    [[NSNotificationCenter defaultCenter]postNotificationName:@"note" object:nil];
    
    
}

-(void)dealloc{
    // [[NSNotificationCenter defaultCenter]removeObserver:self];
    [[NSNotificationCenter defaultCenter]removeObserver:self.observe];
    
    
}

#pragma mark 1、正常的通知
-(void)note1{
    
    /**
     添加通知
     observer:观察者
     aSelector:只要一监听到通知就会调用观察者这个方法
     aName:通知名称
     anObject:谁发出的通知
     - (void)addObserver:(id)observer selector:(SEL)aSelector name:(nullable NSNotificationName)aName object:(nullable id)anObject;
     */
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(reciveNote:) name:@"note" object:nil];
    
    /**
     发送通知
     aName:通知名称
     anObject:谁发出的通知
     - (void)postNotificationName:(NSNotificationName)aName object:(nullable id)anObject;
     */
    [[NSNotificationCenter defaultCenter]postNotificationName:@"note" object:nil];
}

@end
